﻿Imports Negocio
Imports DataLayer
Imports Excepciones

Public Class GesIdiomas

    Private Shared Path As String = "Languages/"
    Public Shared Function Obtener(user As Usuario) As Idioma
        Dim i As New Idioma()

        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "IDIOMAS_PROC"
            repository.AddParameter("@ACCION", "byUSER")
            repository.AddParameter("@ID", Nothing)
            repository.AddParameter("@NOMBRE", user.Nick)
            repository.AddParameter("@ARCHIVO", Nothing)
            Dim dtIdioma As DataTable = repository.ExecuteDataTable()

            For Each dr As DataRow In dtIdioma.Rows
                i = Deserialize(dr.Item("idiARCHIVO").ToString())
            Next

        Catch ex As Exception
            Throw New LanguageGetException()
        End Try
        Return i
    End Function

    Public Shared Sub Asignar(user As Usuario, idioma As Idioma)
        user.Idioma = idioma
    End Sub

    Public Shared Sub Guardar(idioma As Idioma)
        Try

            If idioma.Persistencia = EstadoPersistencia.INSERT Then
                idioma.Archivo = "Language_" & idioma.Nombre & ".xml"
            End If

            Dim repository As IRepository = RepositoryFactory.CreateTransactional()

            repository.Nombre = "IDIOMAS_PROC"
            repository.AddParameter("@ACCION", idioma.Persistencia)
            repository.AddParameter("@ID", idioma.OID)
            repository.AddParameter("@NOMBRE", idioma.Nombre)
            repository.AddParameter("@ARCHIVO", idioma.Archivo)

            If idioma.Persistencia = EstadoPersistencia.INSERT Then
                idioma.OID = Convert.ToInt32(repository.ExecuteReturnValue())
                idioma.Persistencia = EstadoPersistencia.UPDATE
            Else
                repository.ExecuteNonQuery()
            End If
            repository.Commit()

            'Si elimina el idioma, borro el archivo
            If idioma.Persistencia = EstadoPersistencia.DELETE Then
                Try
                    IO.File.Delete(Path & idioma.Archivo)
                Catch ex As Exception

                End Try
            Else
                Serialize(idioma)
            End If


        Catch ex As Exception
            Throw New LanguageSaveException()
        End Try
        GesBitacoras.Guardar(idioma.Persistencia, "ModIdioma", New String() {"Idioma", idioma.Nombre})
    End Sub
    Public Shared Sub Eliminar(idioma As Idioma)
        idioma.Persistencia = EstadoPersistencia.DELETE
        Guardar(idioma)
    End Sub

    Public Shared Function Consultar(nombre As String) As List(Of Idioma)

        Dim idiomas As New List(Of Idioma)
        Try
            Dim repository As IRepository = RepositoryFactory.Create()
            repository.Nombre = "IDIOMAS_PROC"
            repository.AddParameter("@ACCION", "4")
            repository.AddParameter("@ID", Nothing)
            repository.AddParameter("@NOMBRE", nombre)
            repository.AddParameter("@ARCHIVO", Nothing)
            Dim dtIdioma As DataTable = repository.ExecuteDataTable()

            For Each dr As DataRow In dtIdioma.Rows
                Try
                    Dim i As New Idioma()
                    i = Deserialize(dr.Item("idiARCHIVO").ToString())
                    idiomas.Add(i)
                Catch ex As Exception

                End Try

            Next

        Catch ex As Exception
            Throw New LanguageGetException()
        End Try

        Return idiomas
    End Function

    Public Shared Function TraduceMensaje(code As String) As Mensaje
        Return Current.Idioma.TraduceMensaje(code)
    End Function

    Public Shared Function Deserialize(file As String) As Idioma

        Try
            Dim x As New Xml.Serialization.XmlSerializer(GetType(Idioma))
            Dim reader As New System.IO.StreamReader(Path & file)
            Dim i As Idioma = DirectCast(x.Deserialize(reader), Idioma)
            reader.Close()
            Return i
        Catch ex As Exception
            Throw New LanguageFileNotFoundException()
        End Try
    End Function

    Public Shared Sub Serialize(idioma As Idioma)
        Try
            Dim x As New Xml.Serialization.XmlSerializer(GetType(Idioma))
            Dim writer As New IO.StreamWriter(Path & idioma.Archivo)
            x.Serialize(writer, idioma)
            writer.Close()
        Catch ex As Exception
            Throw New LanguageSaveException
        End Try
    End Sub


End Class
